package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.SalesDao;
import org.lq.ht.entity.Customer;
import org.lq.ht.entity.Sales;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;

/**
 * @author 孙少阳
 * @package org.lq.ht.dao.impl
 * @date 2020年8月20日下午11:20:05
 */

public class SalesDaoImpl implements SalesDao,Instantiation<Sales>{

	@Override
	public int save(Sales t) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			String sql = "insert into sales(order_num,vip_card,room_id,customer_id,goods_name,goods_num,goods_unit,goods_price,come_date,leavl_date,bill_date,note) values(?,?,?,?,?,?,?,?,?,?,?,?)";
			num = JDBCUtil.executeUpdate(sql,t.getOrder_num(),t.getVip_card(),t.getRoom_id(),t.getCustomer_id(),t.getGoods_name(),t.getGoods_num(),t.getGoods_unit(),t.getGoods_price(),t.getCome_date(),t.getLeave_date(),t.getNote());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int update(Sales t) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			String sql = "update  sales  set order_num=?,vip_card=?,room_id=?,customer_id=?,goods_name=?,goods_num=?,goods_unit=?,goods_price=?,come_date=?,leavl_date=?,bill_date=?,note=? where no=?";
			num = JDBCUtil.executeUpdate(sql,t.getOrder_num(),t.getVip_card(),t.getRoom_id(),t.getCustomer_id(),t.getGoods_name(),t.getGoods_num(),t.getGoods_unit(),t.getGoods_price(),t.getCome_date(),t.getLeave_date(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(int no) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			String sql = "delete from sales where no = ?";
			num = JDBCUtil.executeUpdate(sql,no);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public List<Sales> findAll() {
		// TODO Auto-generated method stub
		List<Sales> list = new ArrayList();
		String sql = "select * from sales";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Sales getById(int no) {
		// TODO Auto-generated method stub
		List<Sales> list = new ArrayList();
		String sql = "select * from sales where no = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this, no);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public Sales instance(ResultSet rs) {
		// TODO Auto-generated method stub
		Sales s = new Sales();
		try {
			s.setOrder_num(rs.getInt("order_num"));
			s.setVip_card(rs.getInt("vip_card"));
			s.setRoom_id(rs.getInt("room_id"));
			s.setCustomer_id(rs.getInt("customer_id"));
			s.setGoods_name(rs.getString("goods_name"));
			s.setGoods_num(rs.getInt("goods_num"));
			s.setGoods_unit(rs.getInt("goods_unit"));
			s.setGoods_price(rs.getDouble("goods_price"));
			s.setCome_date(rs.getTimestamp("come_date"));
			s.setLeave_date(rs.getTimestamp("leave_date"));
			s.setBill_date(rs.getTimestamp("bill_date"));
			s.setNote(rs.getString("note"));			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return s;
	}

}
